/*
 * 楼盘基本信息
 * @author 狮子
 * @time 2018/9/19
 * @since 1.0.0
 * */
// React基础组件
import React from 'react'
// dva 连接组件 - 连接route和model
import { connect } from 'dva'
// 引入antd的组件
import { Form, Modal, Row, Col, Spin, Select, Tabs, Radio } from 'antd'
//导入路由组件，用于跳转等
import { Link, hashHistory } from 'dva/router'
// 导入项目全局配置文件
import config from 'config/config'
// 引入model取值工具方法
import { mapStateToProps } from 'utils/view_utils'
//引用listPage
import ListPage from 'widget/PageTmpl/ListPage'
import cx from 'classnames'
import FlagBtn from 'widget/FlagBtn/FlagView'
import { download } from "utils/util"

const confirm = Modal.confirm;
const Option = Select.Option;
const FormItem = Form.Item;
const { TabPane } = Tabs;
const RadioGroup = Radio.Group;
const { zysqAppKey,zysqAppsecret} = config
/**
 * description 路由视图及事件处理
 * dispatch 分发到models中的effects对应事件名
 * model 用于获取models中state定义的数据
 * form 表单对象
 */
const RouteView = function ({ dispatch, model, form }) {
  // 表单的子函数
  const { resetFields, getFieldDecorator, validateFields, setFieldsValue } = form
  // 取出model对象，命名空间
  const { modelObj, namespace } = model

  const { biz, loading} = modelObj
  function detailSkip(action ,type, id) {
    hashHistory.push({
      pathname: '/home/buildingAdmin/forRentEdit',
      query: { actionType:action, type, id }
    });

  }

  function showDeleteConfirm(id) {
    confirm({
      title: '您确定删除吗?',
      content: '删除后不可恢复',
      okText: '确定',
      okType: 'danger',
      cancelText: '取消',
      onOk() {
        console.log('OK');
        console.log(`${namespace}`)
        dispatch({
          type: `${namespace}/deleteRentHouse`,
          payload: { id }
        });
      },
      onCancel() {
        console.log('Cancel');
      },
    });
  }

  function updateModal(type) {
    dispatch({
      type: `${namespace}/updateStore`,
      payload: { shareModal: type }
    })
  }
  function getDirection(item){
    if (item == '0'){
      return '东'
    }else if(item == '1'){
      return '南'
    }else if(item == '2'){
      return '西'
    }else if(item == '3'){
      return '北'
    }else{
      return ''
    }
  }
  function getDecoration(item){
    if (item == '0'){
      return '毛坯'
    }else if(item == '1'){
      return '简装'
    }else if(item == '2'){
      return '精装'
    }else{
      return ''
    }
  }
  function getElevator(item){
    if (item == '0'){
      return '无'
    }else if(item == '1'){
      return '有'
    }else{
      return ''
    }
  }
  function getPropertyType(item){
    if (item == '0'){
      return '普通住宅'
    }else if(item == '1'){
      return '公寓'
    }else if(item == '2'){
      return '别墅'
    }else if(item == '3'){
      return '平房'
    }else if(item == '4'){
      return '商住楼'
    }else if(item == '5'){
      return '其他'
    }else{
      return ''
    }
  }
  function getAudit(item){
    if(!item){
      return '未审核'
    }else if(item == '1'){
      return '通过'
    }else if(item == '2'){
      return '拒绝'
    }else{
      return ''
    }
  }
  // 分页表格属性配置
  let pageProps = {
    // ---------- 界面部分 ----------
    ui: {
      // 页面接口地址
      api_url: "admin/rent/list",
      // 接口方式
      method: "POST",
      headers: {
        "Content-type": "application/json"
      },
      app: "",
      // 接口参数
      params: {'type': 3},
      // 模拟数据 - 用于开发需要，若填充了上面的api_url之后，请将此项设置为null
      // "mockData": mockData,
      mockData: null,
      // 查询栏
      search_bar: {
        fields: [
          {
            en_name: 'name',
            zh_name: '小区名称',
            elem_type: 'Input',
            elem_valid_type: 'string',
          },
        ],
        searchHandler: (e) => { // 自定义搜索按钮回调函数，点击搜索时触发
          console.log('search click', e)
        },
        resetHandler: () => { // 自定义重置按钮回调函数，点击重置时触发
          console.log('reset click')
        },
        searchCallback: params => { // 自定义搜索完成后的后调函数, 搜索完成后触发, 并将本次搜索的参数传入
          console.log('search finish', params)
        },
        pasteTargetList: [  // 列表组件中将输入框的en_name作为默认id(也可以通过id另外设置)
          'carName',  // 将需要粘贴事件拦截的输入框id以数组的形式传入, 该参数对应PasteFromClipboard组件的targetList参数
        ]
      },
      // 数据表格
      "table":
      {
        status_params: "status=0",
        status_text: "全部",
        // 表头字段列表
        scroll: { x: 1700 },
        fields: [
          {
            en_name: "id",
            zh_name: "序号",
            with: 90
          },
          {
            en_name: "name",
            zh_name: "小区名称",
            width: 180,
          },
          {
            en_name: "address",
            zh_name: "小区地址",
            width: 150
          },
          {
            en_name: "area",
            zh_name: "建筑面积(m²)",
            width: 110,
          },
          {
            en_name: "floor",
            zh_name: "楼层",
            width: 120,
          },
          {
            en_name: "direction",
            zh_name: "朝向",
            width: 110,
            render: (item) => {
              return (
                <span>{ getDirection(item)}</span>
              )
            },
          },
          {
            en_name: "decoration",
            zh_name: "装修",
            width: 110,
            render: (item) => {
              return (
                <span>{ getDecoration(item)}</span>
              )
            },
          },
          {
            en_name: "elevator",
            zh_name: "电梯",
            width: 110,
            render: (item) => {
              return (
                <span>{ getElevator(item)}</span>
              )
            },
          },
          {
            en_name: "propertyType",
            zh_name: "房屋类型",
            render: (item) => {
              return (
                <span>{ getPropertyType(item)}</span>
              )
            },
            width: 110,
          },
          {
            en_name: "price",
            zh_name: "期望租金",
            width: 110,
          },
          {
            en_name: "contact",
            zh_name: "联系人",
            width: 100,
          },
          {
            en_name: "mobile",
            zh_name: "联系方式",
          },
          {
            en_name: "createTime",
            zh_name: "发布时间",
            width: 190
          }
          
        ],
        // 表格操作
        actions: [
        {
          func_name: 'onToggleStatus',
          render: (item) => {
            return <FlagBtn node={<a onClick={() => detailSkip('edit', 3, item.id)}>查看</a>} btnId={'10103'}></FlagBtn>
          }
        },
        {
          func_name: 'onToggleStatus',
          render: (item) => {
            return <FlagBtn node={<a onClick={() => showDeleteConfirm(item.id)}>删除</a>} btnId={'10103'}></FlagBtn>
          }
        },
        ],
        action_props: {
          fixed: "right",
          width: 200
        }
      },
      action_bar: [{
        label: '新建人才公寓',
        type: 'primary',
        btnId: '10101',
        onClick: (e, row) => {
          detailSkip('add', 3, null)
        }
      }]
    },
  }
  if (!!modelObj.biz) {
    pageProps.biz = modelObj.biz
    pageProps.biz.syncBackCbf = (biz) => {
      //console.log(biz)
      dispatch({
        type: `${namespace}/updateStore`,
        payload: { biz }
      })
    }
  }
  return (
    <div>
      <div className={cx('pageLayout')}>
        <ListPage pageProps={pageProps} />
      </div>
    </div>

  )
}

// 连接视图(view)和模型(model)
export default connect(mapStateToProps)(Form.create()(RouteView))
